#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

int get_mid(int* a, int begin, int end)
{
	int mid = (begin + end) / 2;
	if (a[begin] < a[mid])
	{
		if (a[mid] < a[end])
		{
			return mid;
		}
		else if (a[begin] < a[end])
		{
			return end;
		}
		else
		{
			return begin;
		}
	}
	//a[begin] > a[mid]
	else
	{
		if (a[mid] > a[end])
		{
			return mid;
		}	
		else if (a[begin] > a[end])
		{
			return end;
		}
		else
		{
			return begin;
		}
	}

}


void Swap(int* p1, int* p2)
{
	int tem = *p1;
	*p1 = *p2;
	*p2 = tem;
}

int _QuickSort(int* a, int begin, int end)
{
	int prev = begin;
	int cur = prev + 1;
	int keyi = begin;
	int mid = get_mid(a, begin, end);
	Swap(&a[mid], &a[keyi]);
	while (cur <= end)
	{
		if (a[cur] < a[keyi] && prev++ != cur)
		{
			Swap(&a[cur], &a[prev]);
		}
		cur++;
	}
	Swap(&a[prev], &a[keyi]);
	keyi = prev;
	return keyi;
}

void QuickSort(int *a, int begin, int end)
{
	int left = begin;
	int right = end;
	if (left >= right)
		return;
	int keyi = _QuickSort(a, begin, end);
	QuickSort(a, begin, keyi - 1);
	QuickSort(a, keyi + 1, end);

}

int main()
{
	int a[10] = { 2,45,6,7,8,9,34,2,1,98 };
	int len = sizeof(a) / sizeof(int);
	QuickSort(a,0,len - 1);
	int i = 0;
	for (i = 0; i < len; i++)
	{
		printf("%d ", a[i]);
	}
	return 0;
}